package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理相关接口
 */
@RestController(value = "adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("执行分页查询套餐：{}",setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageQuerySetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加套餐
     * @return
     */
    @PostMapping("")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @ApiOperation("添加套餐")
    public Result<String> addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("执行添加套餐：{}",setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐售卖状态
     * 含有停售菜品的套餐不能起售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)// 清除所有缓存数据
    @ApiOperation("修改套餐售卖状态")
    public Result<String> switchSetmealStatus(@PathVariable Integer status,Long id){
        log.info("修改{}套餐售卖状态:{}",id,status);
        setmealService.switchSetmealStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id获取套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取套餐信息")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        log.info("根据id获取套餐信息:{}",id);
        SetmealVO setmealVO=setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐=修改setmeal+setmeal_dish
     * @return
     */
    @PutMapping("")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)// 清除所有缓存数据
    @ApiOperation("修改套餐信息")
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐相关信息");
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐
     * 起售中的套餐不能删除
     * @param ids
     * @return
     */
    @DeleteMapping("")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)// 清除所有缓存数据
    @ApiOperation("删除套餐")
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info("删除套餐");
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }
}
